import json
from copy import deepcopy

import requests

from api_py.entity.request import Request
from api_py.entity.response import Response
from api_py.enums.cookie import Cookie
from api_py.enums.method import Method
from api_py.test.settings import base_headers, base_url
from api_py.utils.MD5 import MD5Encode

error_cookie = "JSESSIONID=BF77831FD5141E5373F09D580BCCOOAD"


# 请求适配器
def send_request_adapter(method, request):
    if method == Method.POST:
        return sendPostRequest(request.url, request.headers, request.params, request.body)

    elif method == Method.GET:
        return sendGetRequest(request.url, request.headers, request.params, request.body)

    elif method == Method.DELETE:
        return sendDeleteRequest(request.url, request.headers, request.params, request.body)

    elif method == Method.PUT:
        return sendPutRequest(request.url, request.headers, request.params, request.body)


# 发送post请求
def sendPostRequest(url, headers, params, body):
    response = requests.post(url, data=json.dumps(body), params=params, headers=headers)
    return Response(response.headers, response.text)


# 发送get请求
def sendGetRequest(url, headers, params, body):
    response = requests.get(url, data=json.dumps(body), params=params, headers=headers)
    return Response(response.headers, response.text)


# 发送delete请求
def sendDeleteRequest(url, headers, params, body):
    response = requests.delete(url, data=json.dumps(body), params=params, headers=headers)
    return Response(response.headers, response.text)


# 发送put请求
def sendPutRequest(url, headers, params, body):
    response = requests.put(url, data=json.dumps(body), params=params, headers=headers)
    return Response(response.headers, response.text)


# 得到一个通过不同用户登录得到的cookie的map
def get_cookies():
    cookies = {}
    admin_cookie = admin_login()
    init(admin_cookie)
    cookies[Cookie.ADMIN_COOKIE.value] = admin_cookie
    cookies[Cookie.SUPERVISOR_COOKIE.value] = supersivor_login()
    cookies[Cookie.COUNSELOR_COOKIE.value] = counselor_login()
    cookies[Cookie.WEIXIN_COOKIE.value] = weixin_login()
    cookies[Cookie.NULL_COOKIE.value] = None
    cookies[Cookie.ERROR_COOKIE.value] = error_login()
    return cookies


def init(cookie):
    # 初始化测试咨询师数据
    insert_counselor(cookie)  # 无论是否存在，尝试插入测试咨询师
    init_counselor(cookie)  # 初始化测试咨询师数据

    # 初始化测试督导数据
    insert_supervisor(cookie)  # 无论是否存在，尝试插入测试咨询师
    init_supervisor(cookie)  # 初始化测试咨询师数据


# 插入测试咨询师
def insert_counselor(cookie):
    body = {
        "username": "counselor",
        "password": "counselor",
        "name": "counselor",
        "age": 58,
        "gender": "女",
        "phoneNumber": "15900000000",
        "idNumber": "350781196403078704",
        "department": "华东师范大学",
        "title": "无",
        "supervisors": [
            "ff80818184524b500184b3feafaa0240",  # 绑定一个督导
        ]
    }
    body['password'] = MD5Encode(body['password'])
    request = Request(base_url + "/user/ms/counselor", deepcopy(base_headers), {}, body)
    request.headers["Cookie"] = cookie
    response = sendPutRequest(request.url, request.headers, {}, request.body)


# 初始化测试咨询师信息
def init_counselor(cookie):
    body = {
        "username": "counselor",
        "password": "counselor",
        "name": "counselor",
        "age": "58",
        "gender": "女",
        "phoneNumber": "15900000000",
        "idNumber": "350781196403078704",
        "department": "华东师范大学",
        "title": "无",
        "email": "a@qq.com",
        "supervisors": [
            "ff80818184524b500184b3feafaa0240",  # 绑定一个督导
        ]
    }
    body['password'] = MD5Encode(body['password'])
    request = Request(base_url + "/user/ms/counselor", deepcopy(base_headers), {}, body)
    request.headers["Cookie"] = cookie
    print(request.headers)
    sendPostRequest(request.url, request.headers, None, request.body)


# 插入测试督导
def insert_supervisor(cookie):
    body = {
        "username": "supervisor",
        "password": "supervisor",
        "name": "supervisor",
        "phoneNumber": "15900000000",
        "idNumber": "350781196403078704",
        "department": "华东师范大学",
        "title": "无",
        "age": "58",
        "gender": "女",
        "qualification": "三级",
        "qualificationCode": "22",
        "email": "a@qq.com",
    }
    body['password'] = MD5Encode(body['password'])
    request = Request(base_url + "/user/ms/supervisor", deepcopy(base_headers), None, body)
    request.headers["Cookie"] = cookie
    sendPutRequest(request.url, request.headers, None, request.body)


# 初始化测试督导信息
def init_supervisor(cookie):
    body = {
        "username": "supervisor",
        "password": "supervisor",
        "name": "supervisor",
        "phoneNumber": "15900000000",
        "idNumber": "350781196403078704",
        "department": "华东师范大学",
        "title": "无",
        "age": "58",
        "gender": "女",
        "qualification": "三级",
        "qualificationCode": "22",
        "email": "a@qq.com",
    }
    body['password'] = MD5Encode(body['password'])
    request = Request(base_url + "/user/ms/supervisor", deepcopy(base_headers), None, body)
    request.headers["Cookie"] = cookie
    sendPostRequest(request.url, request.headers, None, request.body)


# 模拟一次管理员登录，用于得到合法的cookie
def admin_login():
    body = {
        "username": "kfcvw50",
        "password": "aaaaaa"
    }
    body['password'] = MD5Encode(body['password'])
    request = Request(base_url + "/auth/login", deepcopy(base_headers), None, body)
    response = sendPostRequest(request.url, request.headers, None, request.body)

    return str(response.headers["Set-Cookie"]).split(";")[0]


# 模拟一次咨询师登录，用于得到合法的cookie
def counselor_login():
    body = {
        "username": "counselor",
        "password": "counselor"
    }
    body['password'] = MD5Encode(body['password'])
    request = Request(base_url + "/auth/login", deepcopy(base_headers), None, body)
    response = sendPostRequest(request.url, request.headers, None, request.body)

    return str(response.headers["Set-Cookie"]).split(";")[0]


# 模拟一次督导登录，用于得到合法的cookie
def supersivor_login():
    body = {
        "username": "supervisor",
        "password": "supervisor"
    }
    body['password'] = MD5Encode(body['password'])
    request = Request(base_url + "/auth/login", deepcopy(base_headers), None, body)
    response = sendPostRequest(request.url, request.headers, None, request.body)

    return str(response.headers["Set-Cookie"]).split(";")[0]


# 模拟登录一次微信，返回cookie
def weixin_login():
    return "JSESSIONID=5F2552E9B5B4A57BAF912BB08D565F33"


# 返回一个错误的cookie
def error_login():
    return error_cookie
